using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace Skywave.Windows.Forms
{
    public partial class Toolbox : UserControl
    {
        public Toolbox()
        {
            InitializeComponent();
        }

        public event EventHandler ToolboxItemClick;

        private bool mNewGroupElapsed = true;
        public bool NewGroupElapsed
        {
            get { return mNewGroupElapsed; }
            set { mNewGroupElapsed = value; }
        }

        public string Title
        {
            get { return toolStripLabelTitle.Text; }
            set { toolStripLabelTitle.Text = value; }
        }

        private Dictionary<string, ToolStripItemCollection> mTabsItems = new Dictionary<string, ToolStripItemCollection>();
        public void AddTab(string tabName, string tabCaption)
        {
            if (mTabsItems.ContainsKey(tabName)) return;
            ToolStripButton tsbTab = new ToolStripButton(tabCaption, imageListGroup.Images[(mNewGroupElapsed) ? 0 : 1]);
            tsbTab.DisplayStyle = ToolStripItemDisplayStyle.ImageAndText;
            tsbTab.TextImageRelation = TextImageRelation.Overlay;
            tsbTab.TextAlign = ContentAlignment.MiddleLeft;
            tsbTab.ImageAlign = ContentAlignment.MiddleRight;
            tsbTab.Name = tabName;
            tsbTab.Tag = mNewGroupElapsed;
            tsbTab.BackColor = Color.LightGray;
            ToolStripSeparator tsp = new ToolStripSeparator();
            mTabsItems.Add(tabName, new ToolStripItemCollection(ts, new ToolStripItem[] { tsbTab, tsp }));
            tsbTab.Click += new EventHandler(tsbTab_Click);
            ts.Items.AddRange(mTabsItems[tabName]);
        }

        void tsbTab_Click(object sender, EventArgs e)
        {
            ToolStripButton tsbTab = sender as ToolStripButton;
            bool curState = (bool)tsbTab.Tag;
            curState = !curState;
            for (int i = 1; i < mTabsItems[tsbTab.Name].Count; i++)
            {
                mTabsItems[tsbTab.Name][i].Visible = curState;
            }
            tsbTab.Image = imageListGroup.Images[(curState) ? 0 : 1];
            tsbTab.Tag = curState;
        }

        public void RemoveTab(string tabName)
        {
            if (mTabsItems.ContainsKey(tabName))
            {
                while (mTabsItems[tabName].Count > 0)
                {
                    ts.Items.Remove(mTabsItems[tabName][0]);
                    mTabsItems[tabName].RemoveAt(0);
                }
                mTabsItems.Remove(tabName);
            }
        }
        public ToolStripItem AddTool(string tabName, string itemName, string itemCaption, object itemTag, Image itemImage)
        {
            if (!mTabsItems.ContainsKey(tabName)) return null;
            if (ts.Items.ContainsKey(itemName)) return null;
            ToolStripButton tsbTool = new ToolStripButton(itemCaption, itemImage);
            tsbTool.Name = itemName;
            tsbTool.Tag = itemTag;
            tsbTool.TextImageRelation = TextImageRelation.ImageBeforeText;
            tsbTool.DisplayStyle = ToolStripItemDisplayStyle.ImageAndText;
            tsbTool.TextAlign = ContentAlignment.MiddleLeft;
            tsbTool.ImageAlign = ContentAlignment.MiddleLeft;
            mTabsItems[tabName].Insert(mTabsItems[tabName].Count - 1, tsbTool);
            tsbTool.Click += new EventHandler(tsbTool_Click);
            ts.Items.AddRange(mTabsItems[tabName]);
            return tsbTool;
        }

        public void AddTool(string tabName, ToolStripItem item)
        {
            if (!mTabsItems.ContainsKey(tabName)) return;
            ToolStripItem tsbTool = item;
            tsbTool.TextImageRelation = TextImageRelation.ImageBeforeText;
            tsbTool.DisplayStyle = ToolStripItemDisplayStyle.ImageAndText;
            tsbTool.TextAlign = ContentAlignment.MiddleLeft;
            tsbTool.ImageAlign = ContentAlignment.MiddleLeft;
            mTabsItems[tabName].Insert(mTabsItems[tabName].Count - 1, tsbTool);
            tsbTool.Click += new EventHandler(tsbTool_Click);
            ts.Items.AddRange(mTabsItems[tabName]);
        }

        public void Clear()
        {
            List<string> keys = new List<string>();
            foreach (string key in mTabsItems.Keys)
            {
                keys.Add(key);
            }
            foreach (string key in keys)
            {
                RemoveTab(key);
            }
        }

        void tsbTool_Click(object sender, EventArgs e)
        {
            if (ToolboxItemClick != null)
                ToolboxItemClick(sender, e);
        }

        private void Toolbox_Resize(object sender, EventArgs e)
        {
            if (ts.PreferredSize.Height < this.Height)
            {
                this.AutoScroll = false;
                ts.Height = this.Height;
            }
            else
            {
                this.AutoScroll = true;
                ts.Height = ts.PreferredSize.Height;
            }
        }

        private void ts_LayoutCompleted(object sender, EventArgs e)
        {
            if (ts.PreferredSize.Height < this.Height)
            {
                this.AutoScroll = false;
                ts.Height = this.Height;
            }
            else
            {
                this.AutoScroll = true;
                ts.Height = ts.PreferredSize.Height;
            }
        }
    }
}
